package com.hyt.it.ogt.kq.main.controller.gov;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.common.apiInfor.DataType;
import com.hyt.common.apiInfor.ParamType;
import com.hyt.it.ogt.kq.common.enums.ArrangeEnum;
import com.hyt.it.ogt.kq.common.gov.config.arrange.CommonLockKeyCreator;
import com.hyt.it.ogt.kq.common.gov.config.swagger.ApiVersionConstant;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.common.gov.constant.StringCodeEnum;
import com.hyt.it.ogt.kq.common.gov.utils.DownloadFileUtil;
import com.hyt.it.ogt.kq.common.gov.utils.GovPage;
import com.hyt.it.ogt.kq.common.gov.utils.StringUtil;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.service.gov.arrange.ArrangeFactory;
import com.hyt.it.ogt.kq.service.gov.model.param.ArrangeParam;
import com.hyt.it.ogt.kq.service.gov.model.param.arrange.*;
import com.hyt.it.ogt.kq.service.gov.model.vo.PlaceRoomVO;
import com.hyt.it.ogt.kq.service.gov.model.vo.arrange.*;
import com.hyt.it.ogt.kq.service.gov.model.vo.examiner.TimeRoomParamVO;
import com.hyt.it.ogt.kq.service.gov.service.IArrangeService;
import com.hyt.log.annotation.Log;
import com.hyt.progress.creator.anno.ProgressAnno;
import com.hyt.swagger.ApiVersion;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@RestController
@Api(tags = "40.编排接口", value = "编排接口")
@ApiSort(value = 40)
@RequestMapping("/kw/gov/arrange")
@Slf4j
public class ArrangeController extends BaseController {
    @Resource
    private ArrangeFactory arrangeFactory;
    @Resource
    private IArrangeService iArrangeService;

    @ApiOperation(value = "40.1.下载自定义编排导入模板")
    @ApiOperationSort(value = 1)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @GetMapping("/getTemplate")
    public ResponseEntity<InputStreamResource> getTemplate() {
        return DownloadFileUtil.downloadClassPathResource("templates/arrange/arrange.xlsx", "自定义编排模板");
    }

    @ApiOperation(value = "40.2.自定义编排导入")
    @ApiOperationSort(value = 2)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @ProgressAnno(total = 100, timeoutSeconds = 60
            * 10, errorCode = 43400101, canConcurrent = false, lockKeyCreator = CommonLockKeyCreator.class)
    @PostMapping(value = "/importExcel")
    public void importExcel(@RequestBody ArrangeParam param) {
        arrangeFactory.arrange(param, ArrangeEnum.ARRANGE_BY_IMPORT);
    }

    @ApiOperation(value = "40.3.生成编排")
    @ApiOperationSort(value = 3)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @Log(module = "编排管理", business = "生成编排")
    @ProgressAnno(total = 100, timeoutSeconds = 60
            * 10, errorCode = 43400301, canConcurrent = false, lockKeyCreator = CommonLockKeyCreator.class)
    @PostMapping(value = "/startArrange")
    public void startArrange(@RequestBody ArrangeParam param) {
        if(StringUtils.isBlank(param.getFileUrl())) {
            Optional<TimeRoomParamVO> first = param.getTimeRoomParamVOList().stream()
                    .filter(item -> StringCodeEnum.ARRANGE_PARAM_ARRANGE_TYPE.getCode().equalsIgnoreCase(item.getCode()))
                    .findFirst();
            if(first.isPresent()){
                String arrageType = first.get().getValue();
                arrangeFactory.arrange(param, "1".equalsIgnoreCase(arrageType) ? ArrangeEnum.ARRANGE_BY_ROOM : ArrangeEnum.ARRANGE_BY_VIRTUAL_ROOM);
            }
        } else {
            arrangeFactory.arrange(param, ArrangeEnum.ARRANGE_BY_IMPORT);
        }
    }



    @ApiOperation(value = "40.4 考生编排统计")
    @ApiOperationSort(value = 4)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @GetMapping(value = "/getArrangeCountResult")
    public ApiResponse<ArrangeResultVO> getArrangeCountResult(String taskId) {
        if(StrUtil.isEmptyIfStr(taskId)) {
            return ApiResponse.<ArrangeResultVO>builder()
                    .code(31400401)
                    .data(null)
                    .build();
        }
        return ApiResponse.<ArrangeResultVO>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .data(iArrangeService.getArrangeCountResult(taskId))
                .build();
    }

    @ApiOperation(value = "40.5 按批次查询编排结果")
    @ApiOperationSort(value = 5)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @GetMapping(value = "/searchArrangeResultByTime")
    public ApiResponse<Page<ArrangeResultByTimeVO>> searchArrangeResultByTime(Page<ArrangeResultByTimeVO> page,
                                                                              ArrangeResultByTimeParam param) {
        return ApiResponse.<Page<ArrangeResultByTimeVO>>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .data(iArrangeService.searchArrangeResultByTime(page, param))
                .build();
    }

    @ApiOperation(value = "40.10 按批次查询编排详细结果")
    @ApiOperationSort(value = 10)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "0-按考场，1-按科目", dataType = DataType.INT, paramType = ParamType.QUERY, required = true),
            @ApiImplicitParam(name = "taskId", value = "任务", dataType = DataType.STRING, paramType = ParamType.QUERY, required = true),
            @ApiImplicitParam(name = "timeId", value = "批次", dataType = DataType.STRING, paramType = ParamType.QUERY, required = true)
    })
    @GetMapping(value = "/getArrangeResultDetailByTime")
    public ApiResponse<List<ArrangeResultDetailByTimeVO>> getArrangeResultDetailByTime(@RequestParam int type,
                                                                                       @RequestParam String taskId,
                                                                                       @RequestParam String timeId) {
        return ApiResponse.<List<ArrangeResultDetailByTimeVO>>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .data(iArrangeService.getArrangeResultDetailByTime(taskId, timeId, type))
                .build();
    }

    @ApiOperation(value = "40.6 按考场查询编排结果")
    @ApiOperationSort(value = 6)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @GetMapping(value = "/searchArrangeResultByRoom")
    public ApiResponse<Page<ArrangeResultByRoomVO>> searchArrangeResultByRoom(Page<ArrangeResultByRoomVO> page,
                                                                              ArrangeResultByRoomParam param) {
        return ApiResponse.<Page<ArrangeResultByRoomVO>>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .data(iArrangeService.searchArrangeResultByRoom(page, param))
                .build();
    }

    @ApiOperation(value = "40.7 按科目查询编排结果")
    @ApiOperationSort(value = 7)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @GetMapping(value = "/searchArrangeResultBySubject")
    public ApiResponse<Page<ArrangeResultBySubjectVO>> searchArrangeResultBySubject(Page<ArrangeResultBySubjectVO> page,
                                                                                    ArrangeResultBySubjectParam param) {
        return ApiResponse.<Page<ArrangeResultBySubjectVO>>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .data(iArrangeService.searchArrangeResultBySubject(page, param))
                .build();
    }

    @ApiOperation(value = "40.8 按考生查询编排结果")
    @ApiOperationSort(value = 8)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @GetMapping(value = "/searchArrangeResultByCandidate")
    public ApiResponse<Page<ArrangeResultByCandidateVO>> searchArrangeResultByCandidate(
            Page<ArrangeResultByCandidateVO> page, ArrangeResultByCandidateParam param) {
        return ApiResponse.<Page<ArrangeResultByCandidateVO>>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .data(iArrangeService.searchArrangeResultByCandidate(page, param))
                .build();
    }

    @ApiOperation(value = "40.9 批次管理-统计科目编排")
    @ApiOperationSort(value = 9)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @GetMapping(value = "/getArrangeSubjectCountResult")
    public ApiResponse<ArrangeSubjectCountVO> getArrangeSubjectCountResult(@RequestParam String taskId) {
        return ApiResponse.<ArrangeSubjectCountVO>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .data(iArrangeService.getArrangeSubjectCountResult(taskId))
                .build();
    }

    @ApiOperation(value = "40.10 重置编排")
    @ApiOperationSort(value = 10)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @Log(module = "编排管理", business = "重置编排")
    @ProgressAnno(total = 100, timeoutSeconds = 60 * 10, errorCode=43401001, canConcurrent = false)
    @PutMapping(value = "/resetArrange")
    public void resetArrange(@RequestParam String taskId) {
        iArrangeService.resetArrange(taskId);
    }

    @ApiOperation(value = "40.11 调整考场")
    @ApiOperationSort(value = 11)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @Log(module = "编排管理", business = "调整考场")
    @ProgressAnno(total = 100, timeoutSeconds = 60 * 10, canConcurrent = false)
    @PutMapping(value = "/changRoom")
    public void changRoom(@RequestBody ChangRoomParam param) {
        iArrangeService.changRoom(param);
    }

    @ApiOperation(value = "40.12 获取可调整的考场")
    @ApiOperationSort(value = 12)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @GetMapping(value = "/getChangeRoom")
    public ApiResponse<GovPage<PlaceRoomVO>> getChangeRoom(GovPage<PlaceRoomVO> page, SearchChangeRoomParam param) {
        return ApiResponse.<GovPage<PlaceRoomVO>>builder().code(ResponseCode.SUCCESS.getCode())
                .data(iArrangeService.getChangeRoom(page, param)).build();
    }

    @ApiOperation(value = "40.13 导出汇总需要的数据包")
    @ApiOperationSort(value = 13)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @GetMapping(value = "/exportOdesPackage")
    @ProgressAnno(total = 100, timeoutSeconds = 60 * 10, errorCode=43401001, canConcurrent = false)
    public void exportOdesPackage(String taskId) throws Exception {
        iArrangeService.downloadPackage(taskId);
    }

    
}
